﻿using CDSysLDManageProject.BYDTableAdapters;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CDSysLDManageProject
{
    public partial class FormNormalProcessSet : Form
    {
        tb_RecipeInfoTableAdapter recipinfo = new tb_RecipeInfoTableAdapter();
        ServTimeGetTableAdapter sertime = new ServTimeGetTableAdapter();
        DataTable dtprocess = new DataTable();
        public FormNormalProcessSet()
        {
            InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            dgvprocess.AutoGenerateColumns = false;
            dgvprocess.AllowUserToAddRows = false;
            dtprocess.Columns.Add("traytype");
            dtprocess.Columns.Add("recipeid");
            dtprocess.Columns.Add("keyid");
            traytype.Items.Add("C12");
            traytype.Items.Add("C15");
            traytype.Items.Add("C19");

            dtprocess = recipinfo.GetRecipeInfo();
            dgvprocess.DataSource = dtprocess;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                dtprocess.Rows.Add(0,"");
            }
            catch (Exception)
            {

                MessageBox.Show("请先完成上一行的编辑!");
            }
        }

        private void btsave_Click(object sender, EventArgs e)
        {
            StaticArg.CheckStatus = "open";
            FormClosedCheck cc = new FormClosedCheck();
            cc.ShowDialog();
            //验证通过了才可以修改
            if (StaticArg.CheckStatus == "close")
            {
                for (int i = 0; i < dgvprocess.RowCount; i++)
                {

                    if (dgvprocess.Rows[i].Cells["recipeid"].Value == null)
                    {
                        //MessageBox.Show("请输入完整的工艺信息!");
                        continue;
                    }
                    if (dgvprocess.Rows[i].Cells["recipeid"].Value.ToString() == "")
                    {
                        MessageBox.Show("请输入电源对应的工艺类型!");
                        return;
                    }

                    //不存在KID则insert
                    if (dgvprocess.Rows[i].Cells["keyid"].Value.ToString() == "0")
                    {
                        DateTime dt = DateTime.Parse(sertime.ScalarQuery().ToString());
                        try
                        {
                            recipinfo.Insert(dgvprocess.Rows[i].Cells["traytype"].Value.ToString(), dgvprocess.Rows[i].Cells["recipeid"].Value.ToString());
                            //pinfo.InsertProcessType(dgvprocess.Rows[i].Cells["processtype"].Value.ToString(), DateTime.Parse(sertime.ScalarQuery().ToString()), null, null);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("将正常工艺设置数据存到数据库失败!");
                            Log.SystemError.Add("将正常工艺设置数据存到数据库失败" + ex.ToString());
                        }

                    }//否则看是否修改
                    else
                    {
                        int ProcessCount = int.Parse(recipinfo.GetRecipeCountByRecipeIdTrayTypeKid(dgvprocess.Rows[i].Cells["recipeid"].Value.ToString(), dgvprocess.Rows[i].Cells["traytype"].Value.ToString(), int.Parse(dgvprocess.Rows[i].Cells["keyid"].Value.ToString())).ToString());
                        if (ProcessCount > 0)
                        {

                        }
                        else
                        {

                            DateTime dt = DateTime.Parse(sertime.ScalarQuery().ToString());
                            try
                            {
                                recipinfo.UpdateRecipeIdByKid(dgvprocess.Rows[i].Cells["recipeid"].Value.ToString(), int.Parse(dgvprocess.Rows[i].Cells["keyid"].Value.ToString()));
                                
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("将正常工艺设定信息同步到数据库失败!");
                                Log.SystemError.Add("将正常工艺设定信息同步到数据库失败" + ex.ToString());
                            }


                        }
                    }

                }
                FormProcessTypeSet_Load(null, null);
            }
            else
            {

            }
            
        }

        private void FormProcessTypeSet_Load(object sender, EventArgs e)
        {
            dtprocess = recipinfo.GetRecipeInfo();
            dgvprocess.DataSource = dtprocess;
        }

        //private void btdelete_Click(object sender, EventArgs e)
        //{
        //    StaticArg.CheckStatus = "open";
        //    FormClosedCheck cc = new FormClosedCheck();
        //    cc.ShowDialog();

        //    if (StaticArg.CheckStatus == "close")
        //    {

        //        if (dgvprocess.SelectedRows.Count >= 1)
        //        {
        //            for (int i = 0; i < dgvprocess.SelectedRows.Count; i++)
        //            {
        //                if (dgvprocess.SelectedRows[i].Cells["processtype"].Value.ToString() == "正常C15" || dgvprocess.SelectedRows[i].Cells["processtype"].Value.ToString() == "正常C19")
        //                {
        //                    MessageBox.Show("不可以删除正常工艺！");
        //                    return;
        //                }

        //                int kid = int.Parse(dgvprocess.SelectedRows[i].Cells["kid"].Value.ToString());
        //                try
        //                {
        //                    pinfo.DeleteByKid(kid);
        //                }
        //                catch (Exception ex)
        //                {

        //                    Log.SystemError.Add("删除工艺名称失败，原因是:" + ex.ToString());
        //                    MessageBox.Show("删除工艺名称失败!");
        //                }
        //            }

        //            //dgvprocess.DataSource = dtprocess;
        //            FormProcessTypeSet_Load(null, null);
        //        }
        //    }
        //    else
        //    {
        //    }
            
        //}
    }
}
